package com.cmys.money.cloudshop.util.extjs;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ExtjsModelUtils {
	private static Map<String,String> models = new HashMap<String,String>();
	
	public static String getModel(String className) throws Exception{
		String model = models.get(className);
		if(model==null||model.length()==0){
			model = createModel(className);
			models.put(className, model);
		}
		return model;
	}
	
	private static String createModel(String className) throws Exception{
		StringBuilder sb = new StringBuilder();
		
		Class clazz = Class.forName(className);
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			String fieldName = field.getName();
			String fieldType = getTypeStr(field.getType());
			boolean sort = false;
			System.out.println(fieldName);
			Annotation[] annotations = field.getAnnotations();
			boolean skip = false;
			for (Annotation annotation : annotations) {
				if(annotation instanceof SkipExtjsModel){
					skip = true;
					break;
				}
				
				if(annotation instanceof ExtjsModel){
					ExtjsModel model = (ExtjsModel)annotation;
					fieldType = getStringByDefault(model.type(),fieldType);
					fieldName = getStringByDefault(model.name(),fieldName);
					sort = model.sort();
				}
			}
			if(skip){
				continue;
			}
			
			sb.append(modelStr(fieldName,fieldType,sort)+",");
		}
		//list set map class
		return delStrSign(sb.toString(),",");
	}
	
	private static String getTypeStr(Class clazz){
		String name = clazz.getName();
		if("int".equals(name)||"java.lang.Integer".equals(name)){
			return "int";
		}else if("byte".equals(name)||"java.lang.Byte".equals(name)){
			return "byte";
		}else if("short".equals(name)||"java.lang.Short".equals(name)){
			return "short";
		}else if("long".equals(name)||"java.lang.Long".equals(name)){
			return "long";
		}else if("double".equals(name)||"java.lang.Double".equals(name)){
			return "double";
		}else if("float".equals(name)||"java.lang.Float".equals(name)){
			return "float";
		}else if("boolean".equals(name)||"java.lang.Boolean".equals(name)){
			return "boolean";
		}else if("char".equals(name)||"java.lang.Char".equals(name)){
			return "char";
		}else if("boolean".equals(name)||"java.lang.Boolean".equals(name)){
			return "boolean";
		}else if("java.lang.String".equals(name)){
			return "string";
		}else if("java.util.Set".equals(name)){
			return "set";
		}else if("java.util.Map".equals(name)){
			return "map";
		}else if("java.util.List".equals(name)){
			return "list";
		}else{
			return name;
		}
	}
	
	private static String modelStr(String name,String type,boolean sort){
		StringBuilder sb = new StringBuilder();
		sb.append("{name:'"+name+"',type:'"+type+"',sort:"+sort+"}");
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	public static String getStringByDefault(String val,String defalutStr){
		if(val==null||val.length()==0){
			return defalutStr;
		}else{
			return val;
		}
	}
	
	public static String delStrSign(String val,String sign){
		if(val.endsWith(val)){
			return val.substring(0,val.length()-sign.length());
		}else{
			return val;
		}
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(ExtjsModelUtils.getModel("com.cm.extjs.UserBean"));
	}
}
